var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
import "./pull-refresh-sfc.css";
import { reactive, computed, getCurrentInstance } from "vue";
import { getElementBounding } from "../utils";
import { usePageLifecycle } from "../hooks";
import FanLoading from "../loading/loading.js";
import { defineComponent } from "vue";
import { createNamespace } from "../utils";
const __default__ = defineComponent({
  name: createNamespace("PullRefresh")[0],
  options: { virtualHost: true }
});
const __vue_sfc__ = /* @__PURE__ */ Object.assign(__default__, {
  props: {
    pullingText: {
      type: String,
      default: "\u4E0B\u62C9\u5373\u53EF\u5237\u65B0..."
    },
    loosingText: {
      type: String,
      default: "\u91CA\u653E\u5373\u53EF\u5237\u65B0..."
    },
    loadingText: {
      type: String,
      default: "\u52A0\u8F7D\u4E2D..."
    },
    successText: {
      type: String,
      default: "\u52A0\u8F7D\u6210\u529F"
    },
    successDuration: {
      type: [Number, String],
      default: 500
    }
  },
  emits: ["refresh"],
  setup(__props, { expose: __expose, emit: __emit }) {
    __expose();
    const emit = __emit;
    const props = __props;
    const { onPageScroll } = usePageLifecycle();
    const state = reactive({
      status: "success",
      // loading pulling loosing success
      height: 50,
      top: 0,
      isTouch: false,
      isPullingGesture: false,
      start: 0,
      startY: 0,
      isTop: true
    });
    const statusTxt = computed(() => {
      let txt = "";
      if (state.status === "pulling") {
        txt = props.pullingText;
      } else if (state.status === "loosing") {
        txt = props.loosingText;
      } else if (state.status === "loading") {
        txt = props.loadingText;
      } else {
        txt = props.successText;
      }
      return txt;
    });
    const _this = getCurrentInstance();
    const getEl = (el) => __async(this, null, function* () {
      const ret = yield getElementBounding({
        selector: el,
        scope: _this
      });
      if (!ret)
        return {};
      return ret;
    });
    const ease = (move) => {
      move = move - state.start;
      if (move > state.height) {
        if (move < state.height * 2) {
          move = state.height + (move - state.height) / 2;
        } else {
          move = state.height * 1.5 + (move - state.height * 2) / 4;
        }
      }
      return Math.round(move);
    };
    const onTouchStart = (e) => __async(this, null, function* () {
      const { top } = yield getEl(".fan-pull-refresh__head");
      state.isTouch = true;
      state.start = e.touches[0].clientY;
      state.startY = top;
    });
    const onTouchMove = (e) => __async(this, null, function* () {
      if (!state.isTop && state.status === "success") {
        state.isPullingGesture = false;
        return;
      }
      const delta = e.touches[0].clientY - state.start;
      if (delta > 0) {
        state.isPullingGesture = true;
        const move = ease(e.touches[0].clientY);
        state.top = move > 0 ? move : 0;
        onScroll();
        e.stopPropagation();
      } else {
        state.isPullingGesture = false;
      }
    });
    const stopPullRefresh = () => {
      state.status = "success";
      setTimeout(() => {
        state.top = 0;
      }, Number(props.successDuration));
    };
    const onTouchEnd = () => {
      if (state.top >= state.height) {
        state.status = "loading";
        state.top = state.height;
        emit("refresh", stopPullRefresh);
      } else {
        state.top = 0;
      }
      state.isTouch = false;
      state.isPullingGesture = false;
    };
    const onScroll = () => {
      const move = Math.round(state.top - state.startY);
      if (move >= state.height) {
        state.status = "loosing";
      } else {
        state.status = "pulling";
      }
    };
    onPageScroll(({ scrollTop }) => {
      if (scrollTop > 0) {
        state.isTop = false;
      } else {
        state.isTop = true;
      }
    });
    const __returned__ = { emit, props, onPageScroll, state, statusTxt, _this, getEl, ease, onTouchStart, onTouchMove, stopPullRefresh, onTouchEnd, onScroll, defineComponent, get createNamespace() {
      return createNamespace;
    }, reactive, computed, getCurrentInstance, get getElementBounding() {
      return getElementBounding;
    }, get usePageLifecycle() {
      return usePageLifecycle;
    }, FanLoading };
    Object.defineProperty(__returned__, "__isScriptSetup", { enumerable: false, value: true });
    return __returned__;
  }
});
import { renderSlot as _renderSlot, createVNode as _createVNode, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, normalizeStyle as _normalizeStyle, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue";
function __vue_render__(_ctx, _cache, $props, $setup, $data, $options) {
  return _openBlock(), _createElementBlock(
    "div",
    {
      class: "fan-pull-refresh",
      style: _normalizeStyle({
        touchAction: $setup.state.isPullingGesture ? "none" : "pan-y"
      }),
      onTouchstartPassive: $setup.onTouchStart,
      onTouchmovePassive: $setup.onTouchMove,
      onTouchend: $setup.onTouchEnd,
      onTouchcancel: $setup.onTouchEnd
    },
    [
      _createElementVNode(
        "div",
        {
          class: "fan-pull-refresh__inner",
          style: _normalizeStyle({
            transform: `translate3d(0, ${$setup.state.top}px, 0)`,
            transitionDuration: $setup.state.isTouch ? "0ms" : "300ms"
          })
        },
        [
          _createElementVNode(
            "div",
            {
              class: "fan-pull-refresh__head fan-flex-center",
              style: _normalizeStyle({ height: `${$setup.state.height}px` })
            },
            [
              $setup.state.status === "loading" ? _renderSlot(_ctx.$slots, "loading", { key: 0 }, () => [
                _createVNode($setup["FanLoading"], {
                  size: "16",
                  style: { "margin-right": "6px" }
                }),
                _createTextVNode(
                  " " + _toDisplayString($setup.statusTxt),
                  1
                  /* TEXT */
                )
              ]) : $setup.state.status === "pulling" ? _renderSlot(_ctx.$slots, "pulling", { key: 1 }, () => [
                _createTextVNode(
                  _toDisplayString($setup.statusTxt),
                  1
                  /* TEXT */
                )
              ]) : $setup.state.status === "loosing" ? _renderSlot(_ctx.$slots, "loosing", { key: 2 }, () => [
                _createTextVNode(
                  _toDisplayString($setup.statusTxt),
                  1
                  /* TEXT */
                )
              ]) : _renderSlot(_ctx.$slots, "success", { key: 3 }, () => [
                _createTextVNode(
                  _toDisplayString($setup.statusTxt),
                  1
                  /* TEXT */
                )
              ])
            ],
            4
            /* STYLE */
          ),
          _renderSlot(_ctx.$slots, "default")
        ],
        4
        /* STYLE */
      )
    ],
    36
    /* STYLE, NEED_HYDRATION */
  );
}
__vue_sfc__.render = __vue_render__;
var stdin_default = __vue_sfc__;
export {
  stdin_default as default
};
